<template>
  <div v-loading="loading">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm">
      <el-row>
        <el-col :span="12">
          <el-form-item :label="$t('指标年度')" prop="indicatorYear">
            <DatePicker v-model="form.indicatorYear" type="year" value-format="yyyy" :placeholder="$t('年')" @change="indicatorYearChange" />
          </el-form-item>
        </el-col>
      </el-row>
      <div class="area-header">
        <span class="area-header-title">{{ $t('指标维护明细') }}</span>
      </div>
      <el-table v-loading="maintainDetailLoading" :data="form.dealIndicatorsMaintainDetailsList" border class="nowrapTable formTable">
        <el-table-column :label="$t('指标名称')" align="center" prop="indicatorName" show-overflow-tooltip>
          <template slot="header" slot-scope="{}">
            <i style="color: #ff4949;margin-right: 4px;">*</i>
            <span>{{ $t('指标名称') }}</span>
          </template>
          <template slot-scope="scope">
            <el-form-item :prop="'dealIndicatorsMaintainDetailsList.' + scope.$index + '.indicatorName'" :rules="indicatorsMaintainDetailRules.indicatorName">
              <el-input v-model="scope.row.indicatorName" show-word-limit :maxlength="100" :placeholder="$t('指标名称')" />
            </el-form-item>
          </template>
        </el-table-column>
        <el-table-column :label="$t('指标方向')" align="center" prop="indicatorDirection">
          <template slot="header" slot-scope="{}">
            <i style="color: #ff4949;margin-right: 4px;">*</i>
            <span>{{ $t('指标方向') }}</span>
          </template>
          <template slot-scope="scope">
            <el-form-item :prop="'dealIndicatorsMaintainDetailsList.' + scope.$index + '.indicatorDirection'" :rules="indicatorsMaintainDetailRules.indicatorDirection">
              <el-select v-model="scope.row.indicatorDirection" filterable :placeholder="$t('指标方向')">
                <el-option
                  v-for="(item, index) in indicatorDirectionOptions"
                  :key="index"
                  :value="item.dictValue"
                  :label="$t(item.dictLabel)"
                />
              </el-select>
            </el-form-item>
          </template>
        </el-table-column>
        <el-table-column :label="$t('年报目标值(万/百分比/个)')" align="center" prop="targetValue">
          <template slot="header" slot-scope="{}">
            <i style="color: #ff4949;margin-right: 4px;">*</i>
            <span>{{ $t('年报目标值(万/百分比/个)') }}</span>
          </template>
          <template slot-scope="scope">
            <el-form-item :prop="'dealIndicatorsMaintainDetailsList.' + scope.$index + '.targetValue'" :rules="indicatorsMaintainDetailRules.targetValue">
              <el-input-number v-model="scope.row.targetValue" :placeholder="$t('年报目标值')" :precision="4" :controls="false" :max="999999999999" />
            </el-form-item>
          </template>
        </el-table-column>
        <el-table-column :label="$t('备注')" align="center" prop="remark">
          <template slot-scope="scope">
            <el-input v-model="scope.row.remark" type="textarea" :placeholder="$t('备注')" show-word-limit :maxlength="2000" :autosize="{ minRows: 2, maxRows: 4}" />
          </template>
        </el-table-column>
        <el-table-column :label="$t('操作')" align="center" width="80">
          <template slot-scope="scope">
            <el-button v-hasPermi="['deal:indicators:remove']" size="mini" type="text" icon="el-icon-delete" @click="delIndicatorsMaintainDetail(scope.$index)">{{ $t('删除') }}</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="tol">
        <el-button v-hasPermi="['deal:indicators:edit']" type="primary" icon="el-icon-plus" size="mini" @click="addIndicatorsMaintainDetail">{{ $t("添加") }}</el-button>
      </div>
    </el-form>
    <div slot="footer" class="dialog-footer" style="text-align: right;">
      <save-btn has-permi="deal:indicators" />
    </div>
  </div>
</template>

<script>
    import { saveDealIndicatorsMaintain, getDealIndicatorsMaintain, getLastIndicatorsMaintainByIndicatorYear } from '@/api/deal/operational/maintain/dealIndicatorsMaintain'
    export default {
        data() {
            return {
                // 表单参数
                form: {},
                // 遮罩层
                loading: false,
              // 指标维护明细遮罩层
              maintainDetailLoading: false,
              // 指标方向
              indicatorDirectionOptions: [],
              companyId: this.$route.query.companyId,
              // 表单校验
              rules: {
                indicatorYear: [
                  { required: true, message: this.$t('必填信息'), trigger: 'blur' }
                ], lastYearAssetRating: [
                  { required: true, message: this.$t('必填信息'), trigger: 'blur' }
                ]
              },
              indicatorsMaintainDetailRules: {
                indicatorName: { required: true, message: this.$t('必填信息'), trigger: 'blur' },
                indicatorDirection: { required: true, message: this.$t('必填信息'), trigger: 'blur' },
                targetValue: { required: true, message: this.$t('必填信息'), trigger: 'blur' }
              }
            }
        },
        created() {
          this.getDicts('indicator_direction').then(response => {
            this.indicatorDirectionOptions = response.data
          })
        },
        methods: {
            // 初始化数据
            init(data) {
              this.loading = true
                if (data != null && data.id !== undefined) {
                    getDealIndicatorsMaintain(data.id).then(response => {
                        this.form = response.data
                        this.loading = false
                    })
                } else {
                    this.reset()
                  this.loading = false
                }
            },
            // 表单重置
            reset() {
                this.form = {
                id: undefined,
                companyId: this.companyId,
                indicatorYear: undefined,
                lastYearAssetRating: undefined,
                createBy: undefined,
                createTime: undefined,
                updateBy: undefined,
                updateTime: undefined,
                delFlag: undefined,
                dealIndicatorsMaintainDetailsList: []
                }
                this.resetForm('form')
            },
            // 取消按钮
            cancel() {
                this.$parent.$parent.formOpen = false
                this.$parent.$parent.reset()
            },
            // 关闭form页面
            closeForm() {
                // 关闭form页面遮罩层
                this.loading = false
                // 关闭form页面
                this.$parent.$parent.formOpen = false
                // 刷新list页面
                this.$parent.$parent.getList()
            },
            /** 提交按钮 */
            submitForm: function() {
                this.$refs['form'].validate(valid => {
                    if (valid) {
                        this.loading = true
                        saveDealIndicatorsMaintain(this.form).then(response => {
                            if (response.code === 200) {
                                this.msgSuccess(this.$t('保存成功'))
                                this.closeForm()
                            } else {
                                this.msgError(response.msg)
                            }
                            this.loading = false
                        }).catch(() => {
                          this.loading = false
                        })
                    } else {
                      this.locationError(document)
                    }
                })
           },
          // 删除指标明细
          delIndicatorsMaintainDetail(index) {
              this.form.dealIndicatorsMaintainDetailsList.splice(index, 1)
          },
          // 添加指标明细
          addIndicatorsMaintainDetail() {
            const row = {
              id: undefined,
              maintainId: undefined,
              indicatorName: undefined,
              indicatorDirection: undefined,
              targetValue: undefined,
              remark: undefined
            }
            if (!(this.form.dealIndicatorsMaintainDetailsList instanceof Array)) {
              this.$set(this.form, 'dealIndicatorsMaintainDetailsList', [])
            }
            this.$nextTick(() => {
              this.form.dealIndicatorsMaintainDetailsList.push(row)
            })
          },
          // 指标年度change事件
          indicatorYearChange(val) {
              this.maintainDetailLoading = true
            const params = {
              indicatorYear: val,
              companyId: this.$route.query.companyId
            }
            getLastIndicatorsMaintainByIndicatorYear(params).then(response => {
              this.form.dealIndicatorsMaintainDetailsList = response.data
              this.form.dealIndicatorsMaintainDetailsList.forEach((item) => {
                item.targetValue = item.targetValue === null ? undefined : item.targetValue
              })
              this.maintainDetailLoading = false
            })
          }
        }
    }
</script>
